/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Br.Com.Cds.Academia.Conta;

import Br.Com.Cds.Academia.DAO.AcessoMySQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Pedro
 */
public class ContaControl {
    
    PreparedStatement pstm;
    ResultSet rs;
    
    
    String consultaConta = "SELECT * FROM CONTA WHERE CONTA LIKE ?";
    String cadastraConta = "INSERT INTO CONTA (CONTA,TIPO,STATUS)VALUES (?,?,?)";
    String alterarConta = "UPDATE CONTA SET CONTA=?,TIPO=?,STATUS=? WHERE IDCONTA=?";
    String excluirConta = "DELETE FROM CONTA WHERE IDCONTA=?"; 
    
     public void cadastraConta (ContaBean conta){
        try {
            AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement) mysql.conectar().prepareStatement(cadastraConta);
            pstm.setString(1,conta.getConta());
            pstm.setString(2, conta.getTipo());
            pstm.setString(3, conta.getStatus());
            pstm.executeUpdate();
            mysql.desconectar();
    
        } catch (SQLException e) {
            e.printStackTrace ();
            JOptionPane.showMessageDialog(null, "SQL EXCEPTION ! ERRO AO TENTAR CADASTRAR UMA CONTA!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE); 
        }
}


    public void alterarBanco (ContaBean conta){
    
      try {
        AcessoMySQL mysql = new AcessoMySQL();
        pstm = (PreparedStatement) mysql.conectar().prepareStatement(alterarConta);
        pstm.setString(1, conta.getConta());
        pstm.setString(2,conta.getTipo());
        pstm.setString(3, conta.getStatus());
        pstm.setInt(4, conta.getIdconta());
        pstm.executeUpdate();
        mysql.desconectar(); 
        } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,"SQL EXCEPTION ! ERRO AO TENTAR ALTERAR UMA CONTA!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
    }
}

    
  public List <ContaBean> listarConta (String nomeConta){
         List <ContaBean> contas = new ArrayList ();
         
         try {
             AcessoMySQL mysql = new AcessoMySQL();
             pstm = (PreparedStatement) mysql.conectar().prepareStatement(consultaConta);
             pstm.setString(1, nomeConta);
             rs = pstm.executeQuery();
             ContaBean alu;
             
             while (rs.next()){
                alu = new ContaBean ();
                alu.setConta(rs.getString("CONTA"));          
                alu.setIdconta(rs.getInt("IDCONTA"));  
                alu.setStatus(rs.getString("STATUS")); 
                alu.setTipo(rs.getString("TIPO"));           
                contas.add(alu);  
             }    
      } catch (Exception e) {
        e.printStackTrace();
         JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR REALIZAR UMA CONSULTA","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
        
      }
        return contas;
      
  }
    
    
  public void excluirBanco (int idConta){
      
      try {
          AcessoMySQL mysql = new AcessoMySQL();
          pstm = (PreparedStatement) mysql.conectar().prepareStatement(excluirConta);
          pstm.setInt(1,idConta);
          pstm.executeUpdate();
          mysql.desconectar();
            
      } catch (Exception e) {
          e.printStackTrace();
          JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR EXCLUIR A CONTA","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
      }

    
}
}